


// 获取 option 管理页面视图
void Request_View_Option(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 登录页面
		LoadPage(c, 200, HTTP_CT_HTML, "option.html");
		
	} else {
		
		// 其他请求方法返回 404 页面
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
		
	}
}



// option 主接口
void Request_Option(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 从配置文件读取设置
		str sPath = xrtPathJoin(2, OptionPath, "global.json");
		xvalue config = xrtParseJSON_File(sPath);
		
		// 返回接口数据
		xvalue tblRet = xvoCreateTable();
		xvoTableSetBool(tblRet, "result", 6, TRUE);
		xvoTableSetText(tblRet, "message", 7, "设置数据获取成功！", 0, FALSE);
		xvoTableSetValue(tblRet, "data", 4, config, TRUE);
		
		// 生成 JSON
		size_t iRetSize = 0;
		char* sRet = xrtStringifyJSON(tblRet, FALSE, &iRetSize);
		http_reply(c, 200, HTTP_CT_JSON, sRet, iRetSize);
		xvoUnref(tblRet);
		
	} else if ( hm->methodCode == HTTP_POST || hm->methodCode == HTTP_PUT ) {
		
		// 保存设置
		xvalue tblForm = xrtParseJSON(hm->body.buf, hm->body.len);
		
		// 写入文件
		str sPath = xrtPathJoin(2, OptionPath, "global.json");
		bool bSuccess = xrtStringifyJSON_File(sPath, tblForm, TRUE);
		
		xvoUnref(tblForm);
		
		if (bSuccess) {
			http_reply(c, 200, HTTP_CT_JSON, "{\"result\": true, \"message\": \"设置保存成功！\"}", 0);
		} else {
			http_reply(c, 200, HTTP_CT_JSON, "{\"result\": false, \"message\": \"设置保存失败！\"}", 0);
		}
		
	} else {
		
		// 其他请求方法返回 404 页面
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
		
	}
}


